﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Ajax_CauHoiAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["Methods"] == null) {
            Response.Write("{\"Status\":\"Error\"}");
            Response.End();
        }
            
        switch (Request["Methods"])
        {
            case "AddCauHoi":
                AddCauHoi();
                break;
            case "UpdateCauHoi":
                UpdateCauHoi();
                break;
            default:
                break;
        }
    }

    public void AddCauHoi()
    {
        string sCauHoi = Request["CauHoi"];
        string sDapAn = Request["DapAn"];

        JavaScriptSerializer serial = new JavaScriptSerializer();
        CauHoiINFO mCauHoi = serial.Deserialize<CauHoiINFO>(sCauHoi);

        List<DapAnINFO> dsDapAn = serial.Deserialize<List<DapAnINFO>>(sDapAn);

        if (CauHoiDAL.ThemCauHoi(mCauHoi, dsDapAn) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể thêm câu hỏi!\"}");
        }
        Response.End();
    }

    public void UpdateCauHoi()
    {
        string sCauHoi = Request["CauHoi"];
        string sDapAn = Request["DapAn"];

        JavaScriptSerializer serial = new JavaScriptSerializer();
        CauHoiINFO mCauHoi = serial.Deserialize<CauHoiINFO>(sCauHoi);

        List<DapAnINFO> dsDapAn = serial.Deserialize<List<DapAnINFO>>(sDapAn);

        if (CauHoiDAL.CapNhatCauHoi(mCauHoi, dsDapAn) > 0)
        {
            Response.Write("{\"Status\":\"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể thêm câu hỏi!\"}");
        }
        Response.End();
    }
}